In many object-oriented programming languages, this
(also called self
or Me
) is a keyword that is used in instance methods to refer to the object on which they are working. C++ and languages which derive in style from it (such as Java, C#, and PHP) generally use this
. Smalltalk and others such as Object Pascal, Python, Ruby, and Objective-C use self
; Visual Basic uses Me
.
The concept is the same in all languages. For brevity, here we just say this
.
this
is usually an immutable reference or pointer which refers to the current object. Some languages, such as Objective-C, allow assignment to this
, although it is deprecated. Doing so can be very misleading to maintenance programmers, because the assignment does not modify the original object, only changing which object that the rest of the code in the method refers to, and can end with undefined behavior.
After an object is properly constructed this
is always a valid reference. Some languages require it explicitly; others use lexical scoping to use it implicitly to bring symbols within their class visible. In the latter case, the use of this
in code, while not illegal, may raise warning bells to a maintenance programmer, although there are still legitimate uses of this
in this case, such as referring to instance variables hidden by local variables of the same name, or if the method wants to return a reference to the current object, i.e. this
, itself.
this
becomes an extra parameter to an instance method. For example, the following instance method in C++
int foo::print (bar x)
is essentially equivalent to the procedural programming:
int foo_print (foo *const this, bar x)
In some languages, for example Python and Perl 5, this
is made explicit, the first parameter of an instance method being such a reference. It needs explicitly to be specified. In this case, the parameter need not necessarily be named this
or self
; like any other parameter, it can be freely named by the programmer; however, by informal convention, the first parameter of an instance method in Perl and Python is named self
.
In some compilers (for example GCC), pointers to C++ instance methods can be directly cast to a pointer of another type, with an explicit this
pointer parameter.[1]
Static methods in C++ or Java are not associated with instances but classes, and so cannot use this
, because there is no object. In others, such as Python, Ruby, Smalltalk or Objective-C, the method is associated with a class object that is passed as this
, and are called class methods.
Contents |
Early versions of C++ would let the this
pointer be changed; by doing so a programmer could change which object a method was working on. This feature was eventually removed, and now this
in C++ is an rvalue.[2]
Early versions of C++ did not include references and it has been suggested that had they been so in C++ from the beginning, this
would have been a reference, not a pointer.[3] But on the other hand, others say that this
being a pointer makes life simpler because it avoids complications that could arise from the class having its address-of operator overloaded.
C++ lets objects destroy themselves with the idiom delete this
.
A Java language keyword that represents the current instance of the class in which it appears. It is used to access class variables and methods.
Since all instance methods are virtual in Java, this
can never be null.
"...the reason for using this construct [this] is that we have a situation that is known as name overloading - the same name being used for two different entities....It is important to understand that the fields and the parameters are separate variables that exist independently of each other, even though they share similar names. A parameter and a field sharing a name is not really a problem in Java." - Objects First with Java, D. Barnes and M. Kölling
this
in C# works the same way as in Java, for reference types. However, within C# "value types", this
has quite different semantics, being similar to an ordinary mutable variable reference, and can even occur on the left side of an assignment.
In Dylan, an OO language that supports multimethods and hasn't a concept of this
, sending a message to an object is still kept in the syntax. The two forms below work the same; the differences are just syntactic sugar.
object.method(param1, param2)
and
method (object, param1, param2)
In Python, there is no keyword for this
, but it exists as the name of the obligatory first argument of all member functions, to which the object instance it is called on is automatically bound. Conventionally, the name self
is used. In class methods (created with the classmethod
decorator), the first argument refers to the class object itself. In static methods (created with the staticmethod
decorator), no special first argument exists.
The Self language is named after this use of "self".
Self
is strictly used within methods of a class. Another way to refer to Self
is to use ::
.